/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demister.sgt.cmd;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 *
 * @author Rock
 */
public class BackgroundCommand extends GraphicsCommand {

    private Color newBGColor;
    private Color oldBGColor;

    public BackgroundCommand() {
        this.name = "Backgournd";
    }

    public BackgroundCommand(Graphics graphics, Color color) {
        super(graphics, "Backgournd");
        this.newBGColor = color;
    }

    public Color getBackgroundColor() {
        return newBGColor;
    }

    public void setBackgroundColor(Color newBGColor) {
        this.newBGColor = newBGColor;
    }

    @Override
    public boolean execute() {
        if (newBGColor == null) {
            return false;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        oldBGColor = g2d.getBackground();
        g2d.setBackground(newBGColor);
        return true;
    }

    @Override
    public void undo() {
        //If execute method is not invoked before, undo is not applicable
        if (oldBGColor == null) {
            return;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.setBackground(oldBGColor);

        oldBGColor = null;
    }

    @Override
    public String getCode() {
        return String.format("((Graphics2D)g).setBackground(new Color(%d, true))", newBGColor.getRGB());
    }
}